package org.example.user.warning.service.impl;

import org.example.commons.entity.SysAdmin;
import org.example.commons.entity.SysMessage;
import org.example.user.warning.repository.SysMessageRepository;
import org.example.user.warning.service.ISysMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author bothin
 */
@Service
public class SysMessageServiceImpl implements ISysMessageService {
    @Autowired
    private SysMessageRepository messageRepository;

    @Override
    public SysMessage queryById(Long id) {
        return messageRepository.findById(id).orElse(null);
    }

    @Override
    public Page<SysMessage> queryAllPage(Pageable pageable) {
        return messageRepository.findAll(pageable);
    }

    @Override
    public List<SysMessage> queryAll() {
        return messageRepository.findAll();
    }

    @Override
    public SysMessage insert(SysMessage entity) {
        return messageRepository.save(entity);
    }

    @Override
    public SysMessage update(SysMessage entity) {
        return messageRepository.save(entity);
    }

    @Override
    public void deleteById(Long id) {
        messageRepository.deleteById(id);
    }

    @Override
    public Page<SysMessage> findUserMessage(Long userId, String status, Pageable pageable) {
        return messageRepository.findUserMessage(userId,status,pageable);
    }

    @Override
    public Page<SysMessage> findUserMessage(Long userId, Pageable pageable) {
        return messageRepository.findUserMessage(userId,pageable);
    }

    @Override
    public Page<SysMessage> findBackendMessage(String status, Pageable pageable) {
        return messageRepository.findBackendMessage(status,pageable);
    }

    @Override
    public Page<SysMessage> findBackendMessage(Pageable pageable) {
        return messageRepository.findBackendMessage(pageable);
    }

    @Override
    public List<SysMessage> findBackendMessage() {
        return messageRepository.findBackendMessage();
    }

    @Override
    public List<SysMessage> findBackendByStatusMessage(String status) {
        return messageRepository.findBackendByStatusMessage(status);
    }

    @Override
    public List<SysMessage> findByMessageIdIn(Long[] ids) {
        return messageRepository.findByMessageIdIn(ids);
    }

    @Override
    public Page<SysMessage> searchList(String name, String title, Date[] dates, String status, Pageable pageable) {
        return messageRepository.searchList("%"+name+"%","%"+title+"%",dates,status,pageable);
    }

    @Override
    public SysMessage updateReadStatus(Long id, String status) {
        SysMessage sysMessage = queryById(id);
        if (sysMessage == null)
            return null;
        sysMessage.setStatus(status);

        return messageRepository.save(sysMessage);
    }
}
